import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class ExecuteCommand {
    public static void main(String[] args) {
        try {
            // 创建进程执行命令，激活虚拟环境并运行Python脚本
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "bash", "-c", "python /Users/dealthknight/Downloads/yolov5-master/detect.py"
            );
            processBuilder.inheritIO(); // 继承IO流，这样就能直接打印输出到控制台

            // 启动进程
            Process process = processBuilder.start();

            // 捕获进程的标准输出和标准错误输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 打印标准输出
            }

            // 捕获错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
                System.err.println(line); // 打印错误输出
            }

            // 等待进程结束
            int exitCode = process.waitFor();
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
